package com.handpay.nocardpay.baselibrary.main.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by lyang on 2017/1/9.
 */

public class IconBaseAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<String> data = null;

    private HashMap<String, PayChannelInfo> mMaps;

    public void setData(ArrayList<String> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public ArrayList<String> getData() {
        return data;
    }


    public IconBaseAdapter(Context context) {
        super();
        mContext = context.getApplicationContext();
        inflater = LayoutInflater.from(context);
        data = new ArrayList<String>();
        mMaps = PayChannelInfo.getPayChannelInfos();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getGridViewItemView() {
        return R.layout.fragment_home_item;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (position < data.size()) {
            if (position == 0 || convertView == null || convertView.getTag() == null) {
                convertView = inflater.inflate(getGridViewItemView(), null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.home_item_img);
                holder.itemsText = (TextView) convertView.findViewById(R.id.home_item_txt);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String id = data.get(position);
            PayChannelInfo info = mMaps.get(id);
            if (info.isSelected) {
                holder.imageView.setBackgroundResource(R.drawable.btn_home_icon_bg);
            } else {
                holder.imageView.setBackgroundResource(android.R.color.transparent);
            }
            holder.itemsText.setText(info.title);
            holder.imageView.setImageResource(info.iconResId[0]);
            if (info.isEnable == false) {
                holder.imageView.setColorFilter(0x9FFFFFFF);
            }else{
                holder.imageView.setColorFilter(android.R.color.transparent);
            }
        }
        return convertView;
    }


    /**
     * ViewHolder类
     */
    static class ViewHolder {
        ImageView imageView;
        TextView itemsText;
    }
}